
#import "../sleipner_config.h"

#ifdef _PLATFORM_IOS


#import "c_inapppurchase.h"
#import "oc_inapppurchase.h"


InAppPurchase* pAppPurchase = nil;

static OnPurchaseSuccess            CallBackOnSuccess = 0;
static OnPurchaseFail               CallBackOnFail = 0;
static int                          IsWaitingForLoadingStore = 0;

void	CLoadStore(const char* ProductName,
                   OnPurchaseSuccess CallBackSucess,
                   OnPurchaseFail    CallBackFail
                   )
{
   
    // [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    
    
    CallBackOnSuccess = CallBackSucess;
    CallBackOnFail = CallBackFail;
    
    pAppPurchase = [InAppPurchase alloc];
     NSString *ProductID  = [[NSString alloc] initWithUTF8String:ProductName];
    [pAppPurchase OCloadStore:ProductID];
}

int	CCanMakePurchases(void)
{
    if( pAppPurchase)
    {
        if( [pAppPurchase OCcanMakePurchases] )
            return 1;
    }
    NSLog(@"InAppPurchase -- > Can't make purchase --> " );
    return 0; 
}

void	CPurchaseProUpgrade(void)
{
    if( pAppPurchase)
    {
        if( [pAppPurchase OCIsLoadingStoreComplete])
        {
            IsWaitingForLoadingStore = 0;
            [pAppPurchase OCpurchaseProUpgrade ];
        }
        else
        {
            IsWaitingForLoadingStore = 1;
        }
    }
}



void    COnSuccess(void)
{
    
    if(CallBackOnSuccess)
    {
        CallBackOnSuccess();
    }
    
    NSLog(@"InAppPurchase -- > Success --> " );
}

void    COnFail(void)
{
      
    if(CallBackOnFail)
    {
        CallBackOnFail();
    }
    
    NSLog(@"InAppPurchase -- > Fail --> " );
}


void       COnCompleteLoadingStore(void)
{
    if(IsWaitingForLoadingStore == 1)
    {
        if(CCanMakePurchases() == 1)
        {
            CPurchaseProUpgrade();
        }
    }
}

void       CRestorePurchase(void)
{
    if(pAppPurchase)
    {
        [pAppPurchase OCRestorePurchase];
    }
    
}




#endif
